package demo.xml.jaxen.custom;

import java.util.List;

import org.jaxen.expr.DefaultXPathFactory;
import org.jaxen.saxpath.helpers.XPathReaderFactory;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import demo.xml.jaxen.Person;


/*
"foo[.='bar']",
"foo[.!='bar']",
"/",
"*",
"//foo",
"/*",
"/.",
"/foo[/bar[/baz]]",
"/foo/bar/baz[(1 or 2) + 3 * 4 + 8 and 9]",
"/foo/bar/baz",
".[1]",
"self::node()",
".",
"count(/)",
"foo[1]",
"/baz[(1 or 2) + 3 * 4 + 8 and 9]",
"foo/bar[/baz[(1 or 2) - 3 mod 4 + 8 and 9 div 8]]",
"foo/bar/yeah:baz[a/b/c and toast]",
"/foo/bar[../x='123']",
"/foo[@bar='1234']",
"foo|bar",
"/foo|/bar[@id='1234']",
"count(//author/attribute::*)",
"$author",
 "10 + $foo",
"10 + (count(descendant::author) * 5)",
"10 + count(descendant::author) * 5",
"2 + (2 * 5)",
"sum(count(//author), 5)",
"sum(count(//author),count(//author/attribute::*))",
"12 + sum(count(//author),count(//author/attribute::*)) div 2",
"text()[.='foo']",
"/* /*[@id='123']",    在实际使用时，这里需要删除掉空格
"/child::node()/child::node()[@id='_13563275']",
"$foo:bar",
"//foo:bar",
"/foo/bar[@a='1' and @c!='2']",
*/
public class Test {
    private void run() throws Exception {
        Person bob = new Person( "bob", 30 );
        bob.addBrother( new Person( "billy", 34 ) );
        bob.addBrother( new Person( "seth", 29 ) );
        bob.addBrother( new Person( "dave", 32 ) );
        bob.addBrother( new Person( "jim", 29 ) );
        bob.addBrother( new Person( "larry", 42 ) );
        bob.addBrother( new Person( "ted", 22 ) );
        
        System.setProperty( XPathReaderFactory.DRIVER_PROPERTY, "" );
        CustomXPath xpath = new CustomXPath( "*" );
        Object object = xpath.selectSingleNode(bob);
//        Object object = xpath.evaluate( bob );
        print(object);
        
//        CustomXPath xpath = new CustomXPath( "brother[position()<4]/name" );
//        List result = (List) xpath.evaluate( bob );
//        Assertions.assertEquals(3, result.size());
        
//        xpath = new CustomXPath( "./brothers[name='dave']" );
//        Object object = xpath.evaluate( bob );
//        print(object);
    }
    
    private static void print(Object object) {
        if(object==null) {
            ConsoleUtils.log("object is null...");
            return;
        }
        ConsoleUtils.log("clazz", object.getClass(), "object", object);
        if(object instanceof List) {
            List items = (List)object;
            ConsoleUtils.log("items.size", items.size());
            for (int i = 0; i < items.size(); i++) {
                Object item = items.get(i);
                ConsoleUtils.log(Integer.toString(i), "clazz", item.getClass(), "item", item);
            }
        }
    }
    
    
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            Test test = new Test();
            test.run();
        } catch (Exception e) {
            e.printStackTrace();
            timer.print("执行总用时");
        }
    }
}
